package picol.model.asa.inst;

import java.util.ArrayList;
import java.util.List;
import picol.model.asa.expr.ASAExpr;

/**
 * Classe de gestion de l'instruction while
 * @author Aurélien Marcel - Julien Sanderson
 */
public class ASAInstWhile extends ASAInst {

    private ASAExpr test;
    private List<ASAInst> listInst;

    public ASAInstWhile() {
        this.listInst = new ArrayList<ASAInst>();
    }

    public void setTest(ASAExpr _test) {
        this.test = _test;
    }

    public void addInst(ASAInst _inst) {
        this.listInst.add(_inst);
    }

    @Override
    public void executer() {
        int evalue = this.test.evalue();

        while (evalue != 0) {
            for (ASAInst inst : this.listInst) {
                inst.executer();
            }

            evalue = this.test.evalue();
        }
    }

    @Override
    public boolean executerDebug() {
        int evalue = this.test.evalue();

        if (evalue != 0) {
            if (this.listInst.iterator().hasNext()) {
                this.listInst.iterator().next().executerDebug();
            }

            evalue = this.test.evalue();
        }
        return true;
    }

    @Override
    public int getNbInst() {
        int nbinst = 0;
        for (ASAInst inst : listInst) {
            nbinst += inst.getNbInst();
            nbinst++;
        }
        return nbinst;
    }
}
